package com.aizuda.boot.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author sangyirong
 * @ClassName ThreadPool
 * @Version 1.0
 * @Date 2025/5/30 14:55
 */
@Slf4j
@Configuration
public class ThreadPool {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        int cores = Runtime.getRuntime().availableProcessors();
        log.info("当前CPU核心数：{}", cores);
        return new ThreadPoolExecutor(cores * 2,
                cores * 2,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(20),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }
}